package com.ch.d0303;

import com.ch.bean.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Login1Servlet", value = "/Login1Servlet")
public class Login1Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        Result result = new Result(0,"",null);
        //验证用户输入的验证码是否正确
        String ucode = request.getParameter("vcode");
        //获取会话中保存的验证码
        Object vcode = request.getSession().getAttribute("vcode");
        if(vcode.equals(ucode) == false){
            result.setMsg("验证码输入错误!");
        }else{
            String name = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            if("yc".equals(name) && "123".equals(pwd)){
                result.setCode(1);
                result.setMsg("登入成功");
                request.getSession().setAttribute("loginedUser",name);
            }else{
                result.setMsg("用户名或密码错误");
            }
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
